package com.cloud.factory;

import cn.hutool.json.JSONUtil;
import com.cloud.constant.Constants;
import com.cloud.constant.RedisKeyConstants;
import com.cloud.pojo.movie.MovieAttr;
import org.springframework.data.redis.core.ListOperations;

import java.util.Random;

/**
 * @version v1.0
 * @author: TianXiang
 * @description: 获取视频信息工厂
 * @date: 2020/11/24
 */
public class MovieInfoFactory {

    public static MovieAttr getMovieInfoInRedis(ListOperations<String, String> opsForList, String strategyType) {
        MovieAttr movieAttr;
        if(Constants.GREEN_MODEL.equals(strategyType)) {
            Long count = opsForList.size(RedisKeyConstants.GREEN_MOVIE_LIST_KEY);
            int random = new Random().nextInt(count.intValue());
            movieAttr = JSONUtil.toBean(opsForList.index(RedisKeyConstants.GREEN_MOVIE_LIST_KEY, random), MovieAttr.class);
        }else {
            Long count = opsForList.size(RedisKeyConstants.MOVIE_LIST_KEY);
            int random = new Random().nextInt(count.intValue());
            movieAttr = JSONUtil.toBean(opsForList.index(RedisKeyConstants.MOVIE_LIST_KEY, random), MovieAttr.class);
        }
        return movieAttr;
    }
}
